import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useAuthStore = defineStore('auth', () => {
  const token = ref(localStorage.getItem('token') || '')
  const username = ref(localStorage.getItem('username') || '')

  const isAuthenticated = computed(() => !!token.value)

  const login = (tokenValue, usernameValue) => {
    token.value = tokenValue
    username.value = usernameValue
    localStorage.setItem('token', tokenValue)
    localStorage.setItem('username', usernameValue)
  }

  const logout = () => {
    token.value = ''
    username.value = ''
    localStorage.removeItem('token')
    localStorage.removeItem('username')
  }

  const checkAuth = () => {
    const storedToken = localStorage.getItem('token')
    const storedUsername = localStorage.getItem('username')
    if (storedToken && storedUsername) {
      token.value = storedToken
      username.value = storedUsername
    }
  }

  return {
    token,
    username,
    isAuthenticated,
    login,
    logout,
    checkAuth
  }
}) 